#include "labelseditdialog.h"
#include "abstractplot2d.h"
#include "ui_labelseditdialog.h"
#include "greekalphabetdialog.h"
#include <QFontDialog>

LabelsEditDialog::LabelsEditDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LabelsEditDialog)
{
    ui->setupUi(this);
    _plotter = 0;

    QListWidgetItem *item_xAxis = new QListWidgetItem(ui->listWidget);
    item_xAxis->setIcon(QIcon(":/img/axisx.png"));
    item_xAxis->setText(tr("Axis X"));
    item_xAxis->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    QListWidgetItem *item_yAxis = new QListWidgetItem(ui->listWidget);
    item_yAxis->setIcon(QIcon(":/img/axisy.png"));
    item_yAxis->setText(tr("Axis Y"));
    item_yAxis->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    QListWidgetItem *item_title = new QListWidgetItem(ui->listWidget);
    item_title->setIcon(QIcon(""));
    item_title->setText(tr("Title"));
    item_title->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    ui->listWidget->setFlow(QListView::TopToBottom);
    ui->listWidget->setCurrentRow(0);

    connect(ui->listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(changeAxis(int)));
    connect(ui->axisFontBtn, SIGNAL(clicked()), this, SLOT(changeNumberFont()));
    connect(ui->applyBtn, SIGNAL(clicked()), this, SLOT(apply()));
    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(changeText(QString)));
    connect(ui->fontBtn, SIGNAL(clicked()), this, SLOT(changeFont()));

    connect(ui->supBtn, SIGNAL(clicked()), this, SLOT(addSupIndex()));
    connect(ui->subBtn, SIGNAL(clicked()), this, SLOT(addSubIndex()));
    connect(ui->boldBtn, SIGNAL(clicked()), this, SLOT(addBold()));
    connect(ui->itBtn, SIGNAL(clicked()), this, SLOT(addItalic()));
    connect(ui->uBtn, SIGNAL(clicked()), this, SLOT(addUnderline()));
    connect(ui->greekAlpBtn, SIGNAL(clicked()), this, SLOT(showGreekAlphabetDlg()));


    greekAlphabetDlg = 0;

}
//-----------------------------------------------------------------------------

LabelsEditDialog::~LabelsEditDialog()
{
    delete ui;
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::setPlotter(AbstractPlot2D *plotter)
{
    if (plotter) {
        _plotter = plotter;
        _xAxisStr = _plotter->nameXAxis();
        _yAxisStr = _plotter->nameYAxis();
        _titleStr = _plotter->title();
        _xAxisFont = _plotter->xAxisFont();
        _yAxisFont = _plotter->yAxisFont();
        _titleFont = _plotter->titleFont();
        _axisNumberFont = _plotter->font();
        ui->numberAxisLabel->setFont(_axisNumberFont);

        changeAxis(0);
    }
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::apply()
{
    _plotter->setXAxisFont(_xAxisFont);
    _plotter->setYAxisFont(_yAxisFont);
    _plotter->setTitleFont(_titleFont);

    _plotter->setNameXAxis(_xAxisStr);
    _plotter->setNameYAxis(_yAxisStr);
    _plotter->setTitle(_titleStr);

    _plotter->setFont(_axisNumberFont);
    _plotter->updatePlot();
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::changeAxis(int currentRow)
{
    switch (currentRow) {
    case 0: { // X Axis
        ui->lineEdit->setText(_xAxisStr);
        ui->previewLabel->setText(_xAxisStr);
        ui->previewLabel->setFont(_xAxisFont);
        break;
        }
    case 1: { // Y Axis
        ui->lineEdit->setText(_yAxisStr);
        ui->previewLabel->setText(_yAxisStr);
        ui->previewLabel->setFont(_yAxisFont);
        break;
        }
    case 2: { // Title
        ui->lineEdit->setText(_titleStr);
        ui->previewLabel->setText(_titleStr);
        ui->previewLabel->setFont(_titleFont);
        break;
        }
    default:
        break;
    }
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::changeNumberFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, _axisNumberFont, this);
    if (ok) {
        _axisNumberFont = font;
        ui->numberAxisLabel->setFont(_axisNumberFont);
    }
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::changeText(const QString &text)
{
    ui->previewLabel->setText(text);
    int index = ui->listWidget->currentRow();
    switch (index) {
    case 0: {
        _xAxisStr = text;
        break;
        }
    case 1: {
        _yAxisStr = text;
        break;
        }
    case 2: {
        _titleStr = text;
        break;
        }
    default:
        break;
    }
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::changeFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, ui->previewLabel->font(), this);
    if (ok) {
        ui->previewLabel->setFont(font);
        int index = ui->listWidget->currentRow();

        switch (index) {
        case 0:
            _xAxisFont = font;
            break;

        case 1:
            _yAxisFont = font;
            break;

        case 2:
            _titleFont = font;
            break;

        default:
            break;
        }
    }
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::addSupIndex()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<sup></sup>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 5;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::addSubIndex()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<sub></sub>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 5;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::addBold()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<b></b>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 3;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::addItalic()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<i></i>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 3;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::addUnderline()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<u></u>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 3;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::showGreekAlphabetDlg()
{
    if (!greekAlphabetDlg) {
        greekAlphabetDlg = new GreekAlphabetDialog(this);
        connect(greekAlphabetDlg, SIGNAL(addSymbol(QString)), this, SLOT(addGreekSymbl(QString)));
    }
    greekAlphabetDlg->show();
}
//-----------------------------------------------------------------------------

void LabelsEditDialog::addGreekSymbl(const QString &str)
{
    int i = ui->lineEdit->cursorPosition();
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += str.size();
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------
